package com.xcm.controller;

import com.github.pagehelper.Page;
import com.xcm.constant.PageConstant;
import com.xcm.model.dto.DeviceGroupListDTO;
import com.xcm.model.vo.SingleDeviceReportVO;
import com.xcm.model.vo.SingleProgramDetailReportVO;
import com.xcm.model.vo.WholeDeviceReportVO;
import com.xcm.model.vo.WholeProgramReportVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceGroupService;
import com.xcm.service.DeviceInfoService;
import com.xcm.service.ProgramJsonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 报表Controller
 */
@Controller
@RequestMapping(value = "/report")
public class ReportController {
	@Autowired
	private ProgramJsonService programJsonService;
	@Autowired
	private DeviceInfoService deviceInfoService;
	@Autowired
	private DeviceGroupService deviceGroupService;
	/**
	 * 单个广告详细报表
	 */
	@RequestMapping(value = "/listSingleProgramDetailReport")
	public String listSingleProgramDetailReport(String receiveStartTime, String receiveEndTime, Long programJsonId, Integer pageNum, Integer pageSize, Model model) {
		if (programJsonId == null) {
			if (StringUtils.isBlank(receiveStartTime) || StringUtils.isBlank(receiveEndTime)) {
				model.addAttribute("total", 0);
				model.addAttribute("pageNum", PageConstant.DEFAULT_PAGE_NUM);
				model.addAttribute("pageSize", PageConstant.DEFAULT_PAGE_SIZE);
				return "single_program_detail_report";
			}
		}
		PageInfo<SingleProgramDetailReportVO> pageInfo = programJsonService.listSingleProgramDetailReportByPage(receiveStartTime, receiveEndTime, programJsonId, pageNum, pageSize);
		model.addAttribute("programDetailList", pageInfo.getList());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		return "single_program_detail_report";
	}
	
	/**
	 * 单个终端的详细报表
	 */
	@RequestMapping(value = "/listSingleDeviceReport")	
	public String listSingleDeviceReport(String startTime, String endTime, String deviceNum, String mac,Integer pageNum, Integer pageSize, Model model) {
		// 无mac地址或机器码默认返回空页面
		if (StringUtils.isBlank(mac) && StringUtils.isBlank(deviceNum)) {
			model.addAttribute("pageNum", PageConstant.DEFAULT_PAGE_NUM);
			model.addAttribute("pageSize", PageConstant.DEFAULT_PAGE_SIZE);
			model.addAttribute("total", 0);
			return "single_device_detail_report";
		}
		Page<SingleDeviceReportVO> pageInfo = deviceInfoService.listSingleDeviceReport(startTime, endTime, deviceNum, mac, pageNum, pageSize);
		model.addAttribute("deviceDetailList", pageInfo.getResult());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		return "single_device_detail_report";
	}


	/**
	 * 整体终端报表
	 */
	@RequestMapping(value = "/listWholeDeviceReport")
	public String listWholeDeviceReport(String startTime, String endTime, String deviceNum,String mac,
										String province,String city, String area,String groupCode,
										Integer pageNum,Integer pageSize, Model model) {
		Page<WholeDeviceReportVO> pageInfo = deviceInfoService.listWholeDeviceReportByPage(startTime, endTime, deviceNum, mac,
				province,city,area,groupCode, pageNum, pageSize);
		model.addAttribute("wholeDeviceReportList", pageInfo.getResult());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("deviceNum", deviceNum);
		model.addAttribute("mac", mac);
		model.addAttribute("province", province);
		model.addAttribute("city", city);
		model.addAttribute("area", area);
		model.addAttribute("groupCode", groupCode);
		//分组下拉
		List<DeviceGroupListDTO> deviceGroupListDTOList = deviceGroupService.listAllDeviceGroup();
		model.addAttribute("deviceGroupListDTOList", deviceGroupListDTOList);
		return "whole_device_report";
	}

	/**
	 * 整体广告
	 * @param timeType 1:接收时间 2:完成时间
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param publishWay 发布方式 1：定时发布(当前只有定时发布) 2:预约发布
	 * @param programJsonId 发布Id
	 * @param pageNum 当前页数
	 * @param pageSize 每页条数
	 * @param model ModelView
	 * @return
	 */
	@RequestMapping(value = "/listWholeProgramReport")
	public String listWholeProgramReport(Integer timeType, String startTime, String endTime, Integer publishWay, Long programJsonId, Integer pageNum, Integer pageSize, Model model) {
		PageInfo<WholeProgramReportVO> pageInfo = programJsonService.listWholeProgramReportByPage(timeType, startTime, endTime, publishWay, programJsonId, pageNum, pageSize);
		model.addAttribute("programList", pageInfo.getList());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("timeType",timeType);
		model.addAttribute("publishWay",publishWay);
		return "whole_program_report";
	}
}
